#!/usr/bin/env python

from news_class import News


def _create_dataset ( start_point, lines, rows, tot_rows ):
        res = {}

        count = 0
        for v in rows:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'rows' ] = tot_rows
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = lines

        return res


def get ( liwe, id_news = 0, permalink = '', affinity = 0, template_name = 'NEWS_SHOW' ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- permalink:		SAFE_DB_STRING
		- affinity:		SAFE_BOOL
		- template_name:	SAFE_DB_STRING
	"""

	n = News ( liwe )

	res = { 'news' : n.get ( id_news = id_news, permalink = permalink, template_name = template_name, admin = 1 ) }

	return res


def create ( liwe, id_categ, title, subtitle = '' , abstract = '', descr = '', tags = "", permalink = '', url = '', url_descr = '', visible = 1, id_news = 0, id_media = 0, pub_date = None, facebook = 0 ):
	"""
	:INPUT:

		- title:		SAFE_STRING
		- subtitle:		SAFE_STRING
		- abstract:		SAFE_HTML
		- descr:		SAFE_HTML
		- visible:		SAFE_NUMBER
		- id_news:		SAFE_NUMBER
		- id_categ:		SAFE_NUMBER
		- id_media:		SAFE_NUMBER
		- pub_date:		SAFE_DATE
		- tags:			SAFE_STRING
		- permalink:		SAFE_STRING
		- url:			SAFE_STRING
		- url_descr:		SAFE_STRING
		- facebook:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify news
	"""

	n = News ( liwe )
	response = n.create ( id_categ = id_categ, title = title, subtitle = subtitle, abstract = abstract,
		         descr = descr, tags = tags, visible = visible,
			 id_news = id_news, id_media = id_media, pub_date = pub_date,
			 permalink = permalink, url = url, url_descr = url_descr, facebook = facebook )

	return { "news": response }

def delete ( liwe, ids ):
	"""
	:INPUT:
		- ids:              SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify news
	"""

	n = News ( liwe )
	n.delete ( ids )

	return { "status": 'ok' }

def set_categ ( liwe, text, parent_id = 0, id_node = None, params = '' ):
	"""
	:INPUT:
		- id_node:              SAFE_NUMBER
		- text:			SAFE_STRING
		- parent_id:		SAFE_NUMBER
		- params		SAFE_DB_STRING

	:PERMS:
		- create:		With this permission you can create/modify news
	"""

	n = News ( liwe )
	return { 'id': n.set_categ ( text, parent_id, id_node, params ) }


def del_categ ( liwe, id_categ ):
	"""
	:INPUT:	
		- id_categ: 	SAFE_STRING
	:PERMS:
		- create:		With this permission you can create/modify news
	"""
	n = News ( liwe )

	res = n.del_category ( id_categ )

	return { "res": res }




def list_categs ( liwe, id_node = None ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- id_node:              SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify news
	"""

	n = News ( liwe )

	rs = n.list_categs ( id_node )
	if not rs: rs = []

	return { 'categs': rs }

def list_am_categories ( liwe,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	
	:xPERMS:
		- admin:
	"""

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES


	n = News ( liwe )
	res = n.list_am_categories ( start_point = start_point, limit = limit )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

	return _create_dataset ( start_point, limit, rows, tot_rows )


def list_am_sub_categories ( liwe, id_parent = 0,  _X_LINES = 10,_X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_parent:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	
	:xPERMS:
		- admin:
	"""

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES


	n = News ( liwe )
	res = n.list_am_sub_categories ( id_parent = id_parent, start_point = start_point, limit = limit )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

	return _create_dataset ( start_point, limit, rows, tot_rows )

def list_old ( liwe, mode = "", quant = 9999999, tags = '', exclude_tags = "" ):
	"""
	:INPUT:
		- mode:		SAFE_STRING
		- tags:		SAFE_STRING
		- quant:	SAFE_NUMBER
	"""
	#quant = int ( quant )

	n = News ( liwe )
	rows = n.list ( True, mode = mode, quant = quant, tags = tags, exclude_tags = exclude_tags ) [ 'rows' ]

	for row in rows:
		# FIXME: fare in modo che se non ci sono immagini
		#	 metta una grafica di default
		try:
			media = row [ 'media' ] [ 0 ]
		except:
			media = { "html" : "Immagine non disp", "ext" : "xxx" } 

		row [ '_img' ] = media [ 'html' ] % { "_size" : "icon", "_ext" : media [ 'ext' ] }

	return { 'news': rows }

def _admin_man_sorting ( ds_sort, ds_cond ):
	
	sort_values = {}
	if ds_cond:
		sort_values [ 'ds_cond' ] = ' '.join ( ds_cond.split ( '|' ) )

	if ds_sort:
		sort_values [ 'ds_sort' ] = ' '.join ( ds_sort.split ( '|' ) )
	return sort_values

def list ( liwe, testo = '',subtitle = '' , pub_date_start = None, pub_date_end = None, id_categs = '', tags = '', visible = 1, title = '', ds_sort = '', ds_cond = '',  _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:

		- testo:		SAFE_STRING
		- subtitle:		SAFE_STRING
		- pub_date_start:       SAFE_DATE
		- pub_date_end:         SAFE_DATE
		- tags:			SAFE_STRING
		- id_categs:		SAFE_STRING
		- visible:		SAFE_DB_BOOL
		- title:		SAFE_STRING
		- ds_sort:		SAFE_DB_STRING
		- ds_cond:		SAFE_STRING
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify news
	"""


	#if ds_cond or ds_sort:
	sort_values = _admin_man_sorting ( ds_sort, ds_cond )

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	n = News ( liwe )
	res = n.search ( title = title,subtitle = subtitle,  testo = testo, pub_date_start = pub_date_start, pub_date_end = pub_date_end, 
			id_categs = id_categs, tags = tags, visible = visible, ds_sort = sort_values.get ( 'ds_sort', '' ), ds_cond = sort_values.get ( 'ds_cond', '' ), start_pos = start_point, limit = limit, admin = 1 )
	
	count = 0
	for v in res [ 'rows' ]:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v

		try:
			media = v [ 'media' ] [ 0 ]
		except:
			media = { "html" : "Immagine non disp", "ext" : "xxx" } 

		v [ '_img' ] = media [ 'html' ] % { "_size" : "icon", "_ext" : media [ 'ext' ] }

		count += 1

	res [ 'rows' ] = res [ 'tot_rows' ]

	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = _X_LINES

	return res

def update_pos ( liwe, id_news = 0, id_categ = 0, pos = 0 ):
	"""
	:INPUT:

		- id_news:		SAFE_NUMBER
		- id_categ:		SAFE_NUMBER
		- pos:			SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify news
	"""
	n = News ( liwe )
	res = n.update_pos ( id_obj = id_news, id_categ = id_categ, pos = pos ) 
	return { 'updated' : res }
